<template>
    <div id="app">
        
    <!--搜索-->
    <el-row>
        <el-col :span="4"><el-input v-model="deptName" size="mini" placeholder="请输入部门名称"></el-input></el-col>
        <el-col :span="4"><el-input v-model="leader"  size="mini" placeholder="请输入负责人"></el-input></el-col>
        <el-col :span="4"><el-input v-model="createBy"  size="mini" placeholder="请输入创建者"></el-input></el-col>
        <el-col :span="4"><el-input v-model="updateBy"  size="mini" placeholder="请输入更新者"></el-input></el-col>
        <el-col :span="4"><el-button type="primary" @click="search"  size="mini" icon="el-icon-search">搜索</el-button>
            &nbsp;<el-button type="primary"  size="mini" icon="el-icon-circle-plus" @click="handleAdd">添加</el-button></el-col>
        <el-col :span="4"><div class="grid-content bg-purple-light">&nbsp;</div></el-col>
    </el-row>
        <!--分页列表显示-->
    <el-row>
        <el-table
                v-loading="loading2"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :data="tableData"
                height="420"
                style="width: 100%"
                :row-class-name="tableRowClassName">
            <el-table-column
                    prop="deptId"
                    label="部门编号">
            </el-table-column>
             <el-table-column
                    prop="deptName"
                    label="部门名称">
            </el-table-column>
            <el-table-column
                    prop="leader"
                    label="负责人">
            </el-table-column>
            <el-table-column
                    prop="phone"
                    label="联系电话">
            </el-table-column>
            <el-table-column
                    prop="email"
                    label="邮箱">
            </el-table-column>
            <el-table-column
                    prop="createBy"
                    label="创建者">
            </el-table-column>
            <el-table-column
                    prop="createTime"
                    label="创建时间">
            </el-table-column>
            <el-table-column
                    prop="updateBy"
                    label="更新者">
            </el-table-column>
            <el-table-column
                    prop="updateTime"
                    label="更新时间">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="状态">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                         @change='changeStatus($event,scope.row.status,scope.row.deptId)'
                        active-color="#13ce66"
                        inactive-color="#ff4949"
                        active-value="0"
                        inactive-value="1"
                        >
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--分页插件-->
    <el-row>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[2,3,5,10,100, 200, 300, 400]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
    </el-row>
    <!--添加和更新 弹框-->
    <el-row>
        <el-dialog :title="title" :visible.sync="dialogFormVisible">
            <el-form :model="form" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="部门编号" :label-width="formLabelWidth" v-if="isShow">
                    <el-input v-model="form.deptId" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="部门名称" :label-width="formLabelWidth" prop="deptName">
                    <el-input v-model="form.deptName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="负责人" :label-width="formLabelWidth">
                    <el-input v-model="form.leader" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" :label-width="formLabelWidth">
                    <el-input v-model="form.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" :label-width="formLabelWidth">
                    <el-input v-model="form.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="是否启用" :label-width="formLabelWidth">
                    <el-switch
                        v-model="form.status"
                         @change='changeStatus($event,form.status,form.deptId)'
                        active-color="#13ce66"
                        inactive-color="#ff4949"
                        active-value="0"
                        inactive-value="1"
                        >
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addOrUpdate">确 定</el-button>
            </div>
        </el-dialog>
    </el-row>
    </div>
</template>
<script>
export default {
    data(){
        return {
            tableData:[],
            currentPage:1,//当前页面，默认1
            pageSize:2,//每页显示条数
            total:100,//分页总数量，默认值随便写，初始化时赋予新值
            dialogFormVisible:false,
            title:'部门添加',
            form:{},
            loading2:"true",  //加载提示
            isShow:true,
            deptName:'',
            leader:'',
            createBy:'',
            updateBy:''
        }

    },
    created:function(){
            this.loading2 = "true";
            //初始化
            this.loadData();
            this.tableRowClassName;
            this.loadRole();
            this.loadDept();
            this.loadMgr();
     },
    methods: {
            //部门状态开关改变
            changeStatus($event,num,deptId){
                console.log(deptId);
                console.log($event);
                console.log(num);
                //定义一个json数据
                var jsons={  
                    deptId:deptId,
                    status:num
                }
                var _this = this;
                
                this.$http({
                    url:'/api/cs/dept/update',
                    method:'post',
                    data:jsons,
                    header:{
                        'Content-Type':'application/json'  //如果写成contentType会报错
                    }
                })
                    .then(function (response) {
                        console.log("=========");
                        console.log(response);
                        if(response.data.code==2000){
                            console.log("用户账号状态改变成功");
                        }else{
                            console.log("用户账号状态改变失败");
                        }
                    })
                    .catch(function (error) {
                        //失败处理
                        console.log(error);
                        console.log("==========");
                    });
            },
           
            //加载部门数据
            loadData(){
                this.$http.post("/api/cs/dept/queryAll?pageNo="+this.currentPage+"&pageSize="+this.pageSize,{
                                            deptName:this.deptName,   //部门名称
                                            leader:this.leader,      //负责人
                                            createBy:this.createBy,  //创建者
                                            updateBy:this.updateBy    //更新者
                                      }).then((response) => {
                                                 console.log(response);
                                              if(response.data.code==2000){
                                                  this.tableData = response.data.data.list;
                                                  this.total = response.data.data.total;
                                                  this.loading2 = "";
                                                  this.mathWay();
                                              }else{
                                                  //失败处理
                                                  console.log(error);
                                              }
                                })
            },
            //各种绑定事件方法
            //每页数量发生变化
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize=val;
                this.loadData();
                /* axios.get('/dept/page',{
                     params: {
                         pageNo: this.currentPage,
                         pageSize: this.pageSize
                     }
                 })
                     .then(function (response) {
                         //正确请求时，回调函数
                         console.log(response);
                         if(response.data.code==2000){
                             _this.tableData = response.data.data.pageDept;
                             _this.total = response.data.data.total;
                         }
                     })
                     .catch(function (error) {
                         //失败处理
                         console.log(error);
                     });*/
            },
            //当前页发生变化
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage=val;
                this.loadData();
                /*axios.get('/dept/page',{
                    params: {
                        pageNo: this.currentPage,
                        pageSize:this.pageSize
                    }
                })
                    .then(function (response) {
                        //正确请求时，回调函数
                        console.log(response);
                        if(response.data.code==2000){
                            _this.tableData = response.data.data.pageDept;
                            _this.total = response.data.data.total;
                        }
                    })
                    .catch(function (error) {
                        //失败处理
                        console.log(error);
                    });*/
            },
            //查询方法
            search(){
                this.loadData();
            },
            //处理添加
            handleAdd(){
                //隐藏添加不需要的
                this.isShow=false;
                //清空原来form
                this.form={};
                //清空多选框选中的
                this.checkedRoles=[];
                //更新显示名称
                this.title="部门添加";
                //对话框显示
                this.dialogFormVisible=true;
            },
            //处理编辑（弹出部门更新对话框）
            handleEdit(index, row) {
                console.log(index, row);
                //显示编辑需要的
                this.isShow=true;
                this.form=row;
                //更新显示名称
                this.title="部门更新";
                //对话框显示
                this.dialogFormVisible=true;
            },

            //提交表单数据（添加或者更新）
            addOrUpdate(){
                var  url ='/api/cs/dept/update';
                if(this.form.deptId==''||this.form.deptId==null){
                    url="/api/cs/dept/insert";
                }
                var _this=this;
                //提交方法
                this.$http.post(url, this.form)
                    .then(function (response) {
                        console.log(response);
                        //提交成功
                        if(response.data.code==2000){
                            //关闭弹出框
                            _this.dialogFormVisible=false;
                            //提示更新成功
                            _this.$message({
                                //  message: '恭喜你，这是一条成功消息',
                                message: response.data.message,
                                type: 'success'
                            });
                            //重新加载数据
                            _this.loadData();
                        }else {
                            _this.$message.error(response.data.message);
                        }
                    })
                    .catch(function (error) {
                        _this.$message.error('错了哦，这是一条错误消息');
                    });
            },
            //处理删除（ 部门删除）
            handleDelete(index, row) {
                console.log(index, row);
                var _this=this;
                //确认弹框
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //执行删除
                   this.$http.delete('/api/cs/dept/deleteById?deptId='+row.deptId)
                        .then(function (response) {
                            //判断删除是否成功
                            if(response.data.code==2000){
                                //提示
                                _this.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                //重新加载数据
                                _this.loadData();
                            }else {
                                _this.$message.error(response.data.message);
                            }
                        })
                        .catch(function (error) {
                            //失败处理
                            console.log(error);
                            _this.$message.error('删除失败!');
                        });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            }
    },

}
</script>
<style>

</style>